package com.doit.day03;

import com.alibaba.fastjson.JSON;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.roaringbitmap.RoaringBitmap;

import java.time.Duration;
import java.util.*;

/**
 * 造数据
 * 练习一下生产者和消费者....
 * 和其他的组件进行小组合==》 redis  mysql   bitmap  布隆过滤器
 */

public class ConsumerExerciseBloomFilter {
    public static void main(String[] args) throws Exception {
        //获取布隆过滤器对象
        BloomFilter<Long> longBloomFilter = BloomFilter.create(Funnels.longFunnel(),100000);


        //1.创建一个消费者==》过
        //开启了一个收集数据的线程
        new Thread(new ConsumerDataThreadBloomFilter(longBloomFilter,"event-log")).start();





    }
}
class ConsumerDataThreadBloomFilter implements Runnable{
    private BloomFilter<Long> longBloomFilter;
    private KafkaConsumer<String, String> consumer;


    public ConsumerDataThreadBloomFilter(BloomFilter<Long> longBloomFilter,String ... topic){
        Map<String, Object> map = new HashMap<>();
        map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        map.put(ConsumerConfig.GROUP_ID_CONFIG,"g02");
        map.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        map.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        map.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        consumer = new KafkaConsumer<String, String>(map);
        consumer.subscribe(Arrays.asList(topic));
        this.longBloomFilter = longBloomFilter;
    }


    @Override
    public void run() {
        while (true){
            //拉取数据
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> record : poll) {
                //获取到kafka中的数据
                String json = record.value();
                EventLog eventLog = JSON.parseObject(json, EventLog.class);
                //去布隆过滤器里面判断在还是不在
                //如果在，将isNew改成0
                //如果不在，将isNew改成1==》一定要将guid标注到布隆过滤器中
                boolean flag = longBloomFilter.mightContain((long) eventLog.getGuid());
                if (flag){
                    //代表在里面，是一个老用户
                    eventLog.setIsNew(0);
                    System.out.println(JSON.toJSONString(eventLog));
                }else {
                    //代表不在里面，是一个新用户
                    eventLog.setIsNew(1);
                    //如果不在，将数据标注到布隆过滤器中
                    longBloomFilter.put((long) eventLog.getGuid());
                    System.out.println(JSON.toJSONString(eventLog));
                }


            }
        }
    }
}
